home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / EDITBUF.CC < prev    next >
C/C++ Source or Header  |  1992-03-30  |  19KB  |  708 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "ioprivate.h"
  19. #include "editbuf.h"
  20. #include <stddef.h>
  21. #ifdef __GNUG__
  22. #pragma implementation "editbuf.h"
  23. #endif
  24.  
  25. /* NOTE: Some of the code here is taken from GNU emacs */
  26. /* Hence this file falls under the GNU License! */
  27.  
  28. // Invariants for edit_streambuf:
  29. // An edit_streambuf is associated with a specific edit_string,
  30. // which again is a sub-string of a specific edit_buffer.
  31. // An edit_streambuf is always in either get mode or put mode, never both.
  32. // In get mode, gptr() is the current position,
  33. // and pbase(), pptr(), and epptr() are all NULL.
  34. // In put mode, pptr() is the current position,
  35. // and eback(), gptr(), and egptr() are all NULL.
  36. // Any edit_streambuf that is actively doing insertion (as opposed to
  37. // replacing) // must have its pptr() pointing to the start of the gap.
  38. // Only one edit_streambuf can be actively inserting into a specific
  39. // edit_buffer; the edit_buffer's _writer field points to that edit_streambuf.
  40. // That edit_streambuf "owns" the gap, and the actual start of the
  41. // gap is the pptr() of the edit_streambuf; the edit_buffer::_gap_start pointer
  42. // will only be updated on an edit_streambuf::overflow().
  43.  
  44. int edit_streambuf::truncate()
  45. {
  46.     str->buffer->delete_range(str->buffer->tell((buf_char*)pptr()),
  47.                   str->buffer->tell(str->end));
  48.     return 0;
  49. }
  50.  
  51. #ifdef OLD_STDIO
  52. inline void  disconnect_gap_from_file(edit_buffer* buffer, FILE* fp)
  53. {
  54.     if (buffer->gap_start_ptr != &fp->__bufp)
  55.     return;
  56.     buffer->gap_start_normal = fp->__bufp;
  57.     buffer->gap_start_ptr = &buffer->gap_start_normal;
  58. }
  59. #endif
  60.  
  61. void edit_streambuf::flush_to_buffer(edit_buffer* buffer)
  62. {
  63.     if (pptr() > buffer->_gap_start && pptr() < buffer->gap_end())
  64.     buffer->_gap_start = pptr();
  65. }
  66.  
  67. void edit_streambuf::disconnect_gap_from_file(edit_buffer* buffer)
  68. {
  69.     if (buffer->_writer != this) return;
  70.     flush_to_buffer(buffer);
  71.     setp(pptr(),pptr());
  72.     buffer->_writer = NULL;    
  73. }
  74.  
  75. buf_index edit_buffer::tell(buf_char *ptr)
  76. {
  77.     if (ptr <= gap_start())
  78.     return ptr - data;
  79.     else
  80.     return ptr - gap_end() + size1();
  81. }
  82.  
  83. #if 0
  84. buf_index buf_cookie::tell()
  85. {
  86.     return str->buffer->tell(file->__bufp);
  87. }
  88. #endif
  89.  
  90. buf_index edit_buffer::tell(edit_mark*mark)
  91. {
  92.     return tell(data + mark->index_in_buffer(this));
  93. }
  94.  
  95. // adjust the position of the gap
  96.  
  97. void edit_buffer::move_gap(buf_offset pos)
  98. {
  99.   if (pos < size1())
  100.     gap_left (pos);
  101.   else if (pos > size1())
  102.     gap_right (pos);
  103. }
  104.  
  105. void edit_buffer::gap_left (int pos)
  106. {
  107.   register buf_char *to, *from;
  108.   register int i;
  109.   int new_s1;
  110.  
  111.   i = size1();
  112.   from = gap_start();
  113.   to = from + gap_size();
  114.   new_s1 = size1();
  115.  
  116.   /* Now copy the characters.  To move the gap down,
  117.      copy characters up.  */
  118.  
  119.   for (;;)
  120.     {
  121.       /* I gets number of characters left to copy.  */
  122.       i = new_s1 - pos;
  123.       if (i == 0)
  124.     break;
  125. #if 0
  126.       /* If a quit is requested, stop copying now.
  127.      Change POS to be where we have actually moved the gap to.  */
  128.       if (QUITP)
  129.     {
  130.       pos = new_s1;
  131.       break;
  132.     }
  133. #endif
  134.       /* Move at most 32000 chars before checking again for a quit.  */
  135.       if (i > 32000)
  136.     i = 32000;
  137.       new_s1 -= i;
  138.       while (--i >= 0)
  139.     *--to = *--from;
  140.     }
  141.  
  142.   /* Adjust markers, and buffer data structure, to put the gap at POS.
  143.      POS is where the loop above stopped, which may be what was specified
  144.      or may be where a quit was detected.  */
  145.   adjust_markers (pos << 1, size1() << 1, gap_size(), data);
  146. #ifndef OLD_STDIO
  147.   _gap_start = data + pos;
  148. #else
  149.   if (gap_start_ptr == &gap_start_normal)
  150.     gap_start_normal = data + pos;
  151. #endif
  152.   __gap_end_pos = to - data;
  153. /*  QUIT;*/
  154. }
  155.  
  156. void edit_buffer::gap_right (int pos)
  157. {
  158.   register buf_char *to, *from;
  159.   register int i;
  160.   int new_s1;
  161.  
  162.   i = size1();
  163.   to = gap_start();
  164.   from = i + gap_end();
  165.   new_s1 = i;
  166.  
  167.   /* Now copy the characters.  To move the gap up,
  168.      copy characters down.  */
  169.  
  170.   while (1)
  171.     {
  172.       /* I gets number of characters left to copy.  */
  173.       i = pos - new_s1;
  174.       if (i == 0)
  175.     break;
  176. #if 0
  177.       /* If a quit is requested, stop copying now.
  178.      Change POS to be where we have actually moved the gap to.  */
  179.       if (QUITP)
  180.     {
  181.       pos = new_s1;
  182.       break;
  183.     }
  184. #endif
  185.       /* Move at most 32000 chars before checking again for a quit.  */
  186.       if (i > 32000)
  187.     i = 32000;
  188.       new_s1 += i;
  189.       while (--i >= 0)
  190.     *to++ = *from++;
  191.     }
  192.  
  193.   adjust_markers ((size1() + gap_size()) << 1, (pos + gap_size()) << 1,
  194.     - gap_size(), data);
  195. #ifndef OLD_STDIO
  196.   _gap_start = data+pos;
  197. #else
  198.   if (gap_start_ptr == &gap_start_normal)
  199.     gap_start_normal = data + pos;
  200. #endif
  201.   __gap_end_pos = from - data;
  202. /*  QUIT;*/
  203. }
  204.  
  205. /* make sure that the gap in the current buffer is at least k
  206.    characters wide */
  207.  
  208. void edit_buffer::make_gap(buf_offset k)
  209. {
  210.   register buf_char *p1, *p2, *lim;
  211.   buf_char *old_data = data;
  212.   int s1 = size1();
  213.  
  214.   if (gap_size() >= k)
  215.     return;
  216.  
  217.   /* Get more than just enough */
  218.   if (buf_size > 1000) k += 2000;
  219.   else k += /*200;*/ 20; // for testing!
  220.  
  221.   p1 = (buf_char *) realloc (data, s1 + size2() + k);
  222.   if (p1 == 0)
  223.     abort(); /*memory_full ();*/
  224.  
  225.   k -= gap_size();            /* Amount of increase.  */
  226.  
  227.   /* Record new location of text */
  228.   data = p1;
  229.  
  230.   /* Transfer the new free space from the end to the gap
  231.      by shifting the second segment upward */
  232.   p2 = data + buf_size;
  233.   p1 = p2 + k;
  234.   lim = p2 - size2();
  235.   while (lim < p2)
  236.     *--p1 = *--p2;
  237.  
  238.   /* Finish updating text location data */
  239.   __gap_end_pos += k;
  240.  
  241. #ifndef OLD_STDIO
  242.   _gap_start = data + s1;
  243. #else
  244.   if (gap_start_ptr == &gap_start_normal)
  245.     gap_start_normal = data + s1;
  246. #endif
  247.  
  248.   /* adjust markers */
  249.   adjust_markers (s1 << 1, (buf_size << 1) + 1, k, old_data);
  250.   buf_size += k;
  251. }
  252.  
  253. /* Add `amount' to the position of every marker in the current buffer
  254.    whose current position is between `from' (exclusive) and `to' (inclusive).
  255.    Also, any markers past the outside of that interval, in the direction
  256.    of adjustment, are first moved back to the near end of the interval
  257.    and then adjusted by `amount'.  */
  258.  
  259. void edit_buffer::adjust_markers(register mark_pointer low,
  260.                  register mark_pointer high,
  261.                  int amount, buf_char *old_data)
  262. {
  263.   register struct edit_mark *m;
  264.   register mark_pointer mpos;
  265.   /* convert to mark_pointer */
  266.   amount <<= 1;
  267.  
  268.   if (_writer)
  269.       _writer->disconnect_gap_from_file(this);
  270.  
  271.   for (m = mark_list(); m != NULL; m = m->chain)
  272.     {
  273.       mpos = m->_pos;
  274.       if (amount > 0)
  275.     {
  276.       if (mpos > high && mpos < high + amount)
  277.         mpos = high + amount;
  278.     }
  279.       else
  280.     {
  281.       if (mpos > low + amount && mpos <= low)
  282.         mpos = low + amount;
  283.     }
  284.       if (mpos > low && mpos <= high)
  285.     mpos += amount;
  286.       m->_pos = mpos;
  287.     }
  288.  
  289.     // Now adjust files
  290.     edit_streambuf *file;
  291.  
  292.     for (file = files; file != NULL; file = file->next) {
  293.     mpos = file->current() - old_data;
  294.     if (amount > 0)
  295.     {
  296.       if (mpos > high && mpos < high + amount)
  297.         mpos = high + amount;
  298.     }
  299.     else
  300.     {
  301.       if (mpos > low + amount && mpos <= low)
  302.         mpos = low + amount;
  303.     }
  304.     if (mpos > low && mpos <= high)
  305.         mpos += amount;
  306.     char* new_pos = data + mpos;
  307.     file->set_current(new_pos, file->is_reading());
  308.     }
  309. }
  310.  
  311. #if 0
  312. stdio_
  313.    __off == index at start of buffer (need only be valid after seek ? )
  314.    __buf ==
  315.  
  316. if read/read_delete/overwrite mode:
  317.      __endp <= min(*gap_start_ptr, edit_string->end->ptr(buffer))
  318.  
  319. if inserting:
  320.      must have *gap_start_ptr == __bufp && *gap_start_ptr+gap == __endp
  321.      file->edit_string->end->ptr(buffer) == *gap_start_ptr+end
  322. if write_mode:
  323.      if before gap
  324. #endif
  325.  
  326. int edit_streambuf::underflow()
  327. {
  328.     if (!(_mode & ios::in))
  329.     return EOF;
  330.     struct edit_buffer *buffer = str->buffer;
  331.     if (!is_reading()) { // Must switch from put to get mode.
  332.     disconnect_gap_from_file(buffer);
  333.     set_current(pptr(), 1);
  334.     }
  335.     buf_char *str_end = str->end->ptr(buffer);
  336.   retry:
  337.     if (gptr() < egptr()) {
  338.     return *gptr();
  339.     }
  340.     if ((buf_char*)gptr() == str_end)
  341.     return EOF;
  342.     if (str_end <= buffer->gap_start()) {
  343.     setg(eback(), gptr(), str_end);
  344.     goto retry;
  345.     }
  346.     if (gptr() < buffer->gap_start()) {
  347.     setg(eback(), gptr(), buffer->gap_start());
  348.     goto retry;
  349.     }
  350.     if (gptr() == buffer->gap_start()) {
  351.     disconnect_gap_from_file(buffer);
  352. //    fp->__offset += fp->__bufp - fp->__buffer;
  353.     setg(buffer->gap_end(), buffer->gap_end(), str_end);
  354.     }
  355.     else
  356.     setg(eback(), gptr(), str_end);
  357.     goto retry;
  358. }
  359.  
  360. int edit_streambuf::overflow(int ch)
  361. {
  362.     if (_mode == ios::in)
  363.     return EOF;
  364.     struct edit_buffer *buffer = str->buffer;
  365.     flush_to_buffer(buffer);
  366.     if (ch == EOF)
  367.     return 0;
  368.     if (is_reading()) { // Must switch from get to put mode.
  369.     set_current(gptr(), 0);
  370.     }
  371.     buf_char *str_end = str->end->ptr(buffer);
  372.   retry:
  373.     if (pptr() < epptr()) {
  374.     *pptr() = ch;
  375.     pbump(1);
  376.     return (unsigned char)ch;
  377.     }
  378.     if ((buf_char*)pptr() == str_end || inserting()) {
  379.     /* insert instead */
  380.     if (buffer->_writer)
  381.         buffer->_writer->flush_to_buffer(); // Redundant?
  382.     buffer->_writer = NULL;
  383.     if  (pptr() >= buffer->gap_end())
  384.         buffer->move_gap(pptr() - buffer->gap_size());
  385.     else
  386.         buffer->move_gap(pptr());
  387.     buffer->make_gap(1);
  388.     setp(buffer->gap_start(), buffer->gap_end());
  389.     buffer->_writer = this;
  390.     *pptr() = ch;
  391.     pbump(1);
  392.     return (unsigned char)ch;
  393.     }
  394.     if (str_end <= buffer->gap_start()) {
  395.     // Entire string is left of gap.
  396.     setp(pptr(), str_end);
  397.     }
  398.     else if (pptr() < buffer->gap_start()) {
  399.     // Current pos is left of gap.
  400.     setp(pptr(), buffer->gap_start());
  401.     goto retry;
  402.     }
  403.     else if (pptr() == buffer->gap_start()) {
  404.     // Current pos is at start of gap; move to end of gap.
  405. //    disconnect_gap_from_file(buffer);
  406.     setp(buffer->gap_end(), str_end);
  407. //    __offset += __bufp - __buffer;
  408.     }
  409.     else {
  410.     // Otherwise, current pos is right of gap.
  411.     setp(pptr(), str_end);
  412.     }
  413.     goto retry;
  414. }
  415.  
  416. void edit_streambuf::set_current(char *new_pos, int reading)
  417. {
  418.     if (reading) {
  419.     setg(new_pos, new_pos, new_pos);
  420.     setp(NULL, NULL);
  421.     }
  422.     else {
  423.     setg(NULL, NULL, NULL);
  424.     setp(new_pos, new_pos);
  425.     }
  426. }
  427.  
  428. // Called by fseek(fp, pos, whence) if fp is bound to a edit_buffer.
  429.  
  430. streampos edit_streambuf::seekoff(streamoff offset, _seek_dir dir,
  431.                   int mode=ios::in|ios::out)
  432. {
  433.     struct edit_buffer *buffer = str->buffer;
  434.     disconnect_gap_from_file(buffer);
  435.     buf_index cur_pos = buffer->tell((buf_char*)current());;
  436.     buf_index start_pos = buffer->tell(str->start);
  437.     buf_index end_pos = buffer->tell(str->end);
  438.     switch (dir) {
  439.       case ios::beg:
  440.     offset += start_pos;
  441.     break;
  442.       case ios::cur:
  443.     offset += cur_pos;
  444.     break;
  445.       case ios::end:
  446.     offset += end_pos;
  447.     break;
  448.     }
  449.     if (offset < start_pos || offset > end_pos)
  450.     return EOF;
  451.     buf_char *new_pos = buffer->data + offset;
  452.     buf_char* gap_start = buffer->gap_start();
  453.     if (new_pos > gap_start) {
  454.     buf_char* gap_end = buffer->gap_end();
  455.     new_pos += gap_end - gap_start;
  456.     if (new_pos >= buffer->data + buffer->buf_size) abort(); // Paranoia.
  457.     }
  458.     set_current(new_pos, is_reading());
  459.     return EOF;
  460. }
  461.  
  462. #if 0
  463. int buf_seek(void *arg_cookie, fpos_t * pos, int whence)
  464. {
  465.     struct buf_cookie *cookie = arg_cookie;
  466.     FILE *file = cookie->file;
  467.     struct edit_buffer *buffer = cookie->str->buffer;
  468.     buf_char *str_start = cookie->str->start->ptr(buffer);
  469.     disconnect_gap_from_file(buffer, cookie->file);
  470.     fpos_t cur_pos, new_pos;
  471.     if (file->__bufp <= *buffer->gap_start_ptr
  472.     || str_start >= buffer->__gap_end)
  473.     cur_pos = str_start - file->__bufp;
  474.     else
  475.     cur_pos =
  476.         (*buffer->gap_start_ptr - str_start) + (file->__bufp - __gap_end);
  477.     end_pos = ...;
  478.     switch (whence) {
  479.       case SEEK_SET:
  480.     new_pos = *pos;
  481.     break;
  482.       case SEEK_CUR:
  483.     new_pos = cur_pos + *pos;
  484.     break;
  485.       case SEEK_END:
  486.     new_pos = end_pos + *pos;
  487.     break;
  488.     }
  489.     if (new_pos > end_pos) {
  490.     seek to end_pos;
  491.     insert_nulls(new_pos - end_pos);
  492.     return;
  493.     }
  494.     if (str_start + new_pos <= *gap_start_ptr &* *gap_start_ptr < end) {
  495.     __buffer = str_start;
  496.         __off = 0;
  497.     __bufp = str_start + new_pos;
  498.     file->__get_limit =
  499.         *buffer->gap_start_ptr; /* what if gap_start_ptr == &bufp ??? */
  500.     } else if () {
  501.     
  502.     }
  503.     *pos = new_pos;
  504. }
  505. #endif
  506.  
  507. /* Delete characters from `from' up to (but not incl) `to' */
  508.  
  509. void edit_buffer::delete_range (buf_index from, buf_index to)
  510. {
  511.   register int numdel;
  512.  
  513.   if ((numdel = to - from) <= 0)
  514.     return;
  515.  
  516.   /* Make sure the gap is somewhere in or next to what we are deleting */
  517.   if (from > size1())
  518.     gap_right (from);
  519.   if (to < size1())
  520.     gap_left (to);
  521.  
  522.   /* Relocate all markers pointing into the new, larger gap
  523.      to point at the end of the text before the gap.  */
  524.   adjust_markers ((to + gap_size()) << 1, (to + gap_size()) << 1,
  525.     - numdel - gap_size(), data);
  526.  
  527.    __gap_end_pos = to + gap_size();
  528.   _gap_start = data + from;
  529. }
  530.  
  531. void edit_buffer::delete_range(struct edit_mark *start, struct edit_mark *end)
  532. {
  533.     delete_range(tell(start), tell(end));
  534. }
  535.  
  536. void buf_delete_chars(struct edit_buffer *buf, struct edit_mark *mark, size_t count)
  537. {
  538.  abort();
  539. }
  540.  
  541. edit_streambuf::edit_streambuf(edit_string* bstr, int mode)
  542. {
  543.     _mode = mode;
  544.     str = bstr;
  545.     edit_buffer* buffer = bstr->buffer;
  546.     next = buffer->files;
  547.     buffer->files = this;
  548.     char* buf_ptr = bstr->start->ptr(buffer);
  549.     _inserting = 0;
  550. //    setb(buf_ptr, buf_ptr, 0);
  551.     set_current(buf_ptr, !(mode & ios::out+ios::trunc+ios::app));
  552.     if (_mode & ios::trunc)
  553.     truncate();
  554.     if (_mode & ios::ate)
  555.     seekoff(0, ios::end);
  556. }
  557.  
  558. // Called by fclose(fp) if fp is bound to a edit_buffer.
  559.  
  560. #if 0
  561. static int buf_close(void *arg)
  562. {
  563.     register struct buf_cookie *cookie = arg;
  564.     struct edit_buffer *buffer = cookie->str->buffer;
  565.     struct buf_cookie **ptr;
  566.     for (ptr = &buffer->files; *ptr != cookie; ptr = &(*ptr)->next) ;
  567.     *ptr = cookie->next;
  568.     disconnect_gap_from_file(buffer, cookie->file);
  569.     free (cookie);
  570.     return 0;
  571. }
  572. #endif
  573.  
  574. edit_streambuf::~edit_streambuf()
  575. {
  576.     if (_mode == ios::out)
  577.     truncate();
  578.     // Unlink this from list of files associated with bstr->buffer.
  579.     edit_streambuf **ptr = &str->buffer->files;
  580.     for (; *ptr != this; ptr = &(*ptr)->next) { }
  581.     *ptr = next;
  582.  
  583.     disconnect_gap_from_file(str->buffer);
  584. }
  585.  
  586. edit_buffer::edit_buffer()
  587. {
  588.     buf_size = /*200;*/ 15; /* for testing! */
  589.     data = malloc(buf_size);
  590.     files = NULL;
  591. #ifndef OLD_STDIO
  592.     _gap_start = data;
  593.     _writer = NULL;
  594. #else
  595.     gap_start_normal = data;
  596.     gap_start_ptr = &gap_start_normal;
  597. #endif
  598.     __gap_end_pos = buf_size;
  599.     start_mark.chain = &end_mark;
  600.     start_mark._pos = 0;
  601.     end_mark.chain = NULL;
  602.     end_mark._pos = 2 * buf_size + 1;
  603. }
  604.  
  605. // Allocate a new mark, which is adjusted by 'delta' bytes from 'this'.
  606. // Restrict new mark to lie within 'str'.
  607.  
  608. edit_mark::edit_mark(struct edit_string *str, long delta)
  609. {
  610.     struct edit_buffer *buf = str->buffer;
  611.     chain = buf->start_mark.chain;
  612.     buf->start_mark.chain = this;
  613.     mark_pointer size1 = buf->size1() << 1;
  614.     int gap_size = buf->gap_size() << 1;
  615.     delta <<= 1;
  616.  
  617.     // check if new and old marks are opposite sides of gap
  618.     if (_pos <= size1 && _pos + delta > size1)
  619.     delta += gap_size;
  620.     else if (_pos >= size1 + gap_size && _pos + delta < size1 + gap_size)
  621.     delta -= gap_size;
  622.  
  623.     _pos = _pos + delta;
  624.     if (_pos < str->start->_pos & ~1)
  625.     _pos = (str->start->_pos & ~ 1) + (_pos & 1);
  626.     else if (_pos >= str->end->_pos)
  627.     _pos = (str->end->_pos & ~ 1) + (_pos & 1);
  628. }
  629.  
  630. // A (slow) way to find the buffer a mark belongs to.
  631.  
  632. edit_buffer * edit_mark::buffer()
  633. {
  634.     struct edit_mark *mark;
  635.     for (mark = this; mark->chain != NULL; mark = mark->chain) ;
  636.     // Assume that the last mark on the chain is the end_mark.
  637.     return (edit_buffer *)((char*)mark - offsetof(edit_buffer, end_mark));
  638. }
  639.  
  640. edit_mark::~edit_mark()
  641. {
  642.     // Must unlink mark from chain of owning buffer
  643.     struct edit_buffer *buf = buffer();
  644.     if (this == &buf->start_mark || this == &buf->end_mark) abort();
  645.     edit_mark **ptr;
  646.     for (ptr = &buf->start_mark.chain; *ptr != this; ptr = &(*ptr)->chain) ;
  647.     *ptr = this->chain;
  648. }
  649.  
  650. int edit_string::length() const
  651. {
  652.     ptrdiff_t delta = end->ptr(buffer) - start->ptr(buffer);
  653.     if (end->ptr(buffer) <= buffer->gap_start() ||
  654.     start->ptr(buffer) >= buffer->gap_end())
  655.     return delta;
  656.     return delta - buffer->gap_size();
  657. }
  658.  
  659. buf_char * edit_string::copy_bytes(int *lenp) const
  660. {
  661.     char *new_str;
  662.     int len1, len2;
  663.     buf_char *start1, *start2;
  664.     start1 = start->ptr(buffer);
  665.     if (end->ptr(buffer) <= buffer->gap_start()
  666.     || start->ptr(buffer) >= buffer->gap_end()) {
  667.     len1 = end->ptr(buffer) - start1;
  668.     len2 = 0;
  669.     start2 = NULL; // To avoid a warning from g++.
  670.     }
  671.     else {
  672.     len1 = buffer->gap_start() - start1;
  673.     start2 = buffer->gap_end();
  674.     len2 = end->ptr(buffer) - start2;
  675.     }
  676.     new_str = (char*)malloc(len1 + len2 + 1);
  677.     memcpy(new_str, start1, len1);
  678.     if (len2 > 0) memcpy(new_str + len1, start2, len2);
  679.     new_str[len1+len2] = '\0';
  680.     *lenp = len1+len2;
  681.     return new_str;
  682. }
  683.  
  684. // Replace the buf_chars in 'this' with ones from 'src'.
  685. // Equivalent to deleting this, then inserting src, except tries
  686. // to leave marks in place: Marks whose offset from the start
  687. // of 'this' is less than 'src->length()' will still have the
  688. // same offset in 'this' when done.
  689.  
  690. void edit_string::assign(struct edit_string *src)
  691. {
  692.     edit_streambuf dst_file(this, ios::out);
  693.     if (buffer == src->buffer /*&& ???*/) { /* overly conservative */
  694.     int src_len;
  695.     buf_char *new_str;
  696.     new_str = src->copy_bytes(&src_len);
  697.     dst_file.sputn(new_str, src_len);
  698.     free (new_str);
  699.     } else {
  700.     edit_streambuf src_file(src, ios::in);
  701.     for ( ; ; ) {
  702.         int ch = src_file.sbumpc();
  703.         if (ch == EOF) break;
  704.         dst_file.sputc(ch);
  705.     }
  706.     }
  707. }
  708.